<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 18-7-4
 * Time: 下午3:06
 */

namespace commen;


use think\Log;
use think\Request;

class ebjyd
{
    //test
//    private static $url = "http://ebjyd.vicp.net/";
//    private static $openid = "123456";
//    private static $openkey = "7fa8282ad93047a4d6fe6111c93b308a";
    //test1
//    private static $url = "http://oil.open.poolfee.com/";
//    private static $openid = "wzbjyd";
//    private static $openkey = "c5dde644a018d9906abcdd04dded12c9";

    private static $url = "http://oil.open.poolfee.com/";
    private static $openid = "llzf";
    private static $openkey = "843991b08003c16355b73c7c75c81b9a";

    static function get_info()
    {
        return self::get_curl("agent", "");
    }
    static function get_cards($page=1,$length=10)
    {
        return self::get_curl("cards", ["page"=>$page,"length"=>$length]);
    }
    static function get_card($card_no)
    {
        return self::get_curl("card", ["card_no"=>$card_no]);
    }
    static function get_apply_order($car_info="")
    {
        return self::get_curl("carApprovalQuery", ["car_info"=>$car_info]);
    }
    static function get_apply_notify()
    {
        $car_info = Request::instance()->post("car_info","","trim");

        return self::get_apply_order($car_info);
    }
    static function get_order($out_trade_no="")
    {
        return self::get_curl("order", ["out_trade_no"=>$out_trade_no]);
    }
    static function get_order_notify()
    {
        $out_trade_no = Request::instance()->post("out_trade_no","","trim");

        return self::get_order($out_trade_no);
    }
    static function recharge($out_trade_no,$card_no,$amount,$notify)
    {
        return self::get_curl("recharge", ["out_trade_no"=>$out_trade_no,"card_no"=>$card_no,"amount"=>$amount,"notify"=>$notify]);
    }
    static function apply($car_info,$mobile,$cardImgURL1,$cardImgURL2,$drivingImgURL1,$drivingImgURL2,$approval_id,$notify)
    {
        $data = [
            "car_info"=>$car_info,
            "mobile"=>$mobile,
            "cardImg1"=>$cardImgURL1,
            "cardImg2"=>$cardImgURL2,
            "drivingImg1"=>$drivingImgURL1,
            "drivingImg2"=>$drivingImgURL2,
            "approvalID"=>$approval_id,
            "notify"=>$notify,
        ];

        return self::get_curl("carApply", $data);
    }
    static function applyDiesel($provider,$cert_no,$type,$approvalImg)
    {
        $data = [
            "provider"=>$provider,
            "cert_no"=>$cert_no,
            "type"=>$type,
            "approvalImg"=>$approvalImg,

        ];
        return self::get_curl("applyDiesel", $data);
    }
    static function get_curl($action, $body)
    {
        $headers = ["Oil-Openid: ".self::$openid];
        $url = self::$url . $action;
        if(is_array($body))
        {
            ksort($body);
            $body["key"] = self::$openkey;
            $body["sign"] = md5(self::ToUrlParams($body));
            unset($body["key"]);
        }
        $info = ["error" => 1, "error_info" => "", "data" => [],"raw_post_data"=>""];
        Log::log(json_encode([$url, $headers, $body]));
        $result= self::curl_url($url, $headers, $body);
        Log::log(json_encode($result));
        $info["raw_post_data"] = json_encode($result);
        if ($result["code"] == 200) {
            $body = json_decode($result["body"], true);
            if(empty($body))
                $info["error_info"] = $result["error_info"];
            else{
                if($body["code"]==200)
                {
                    $info["error"] = 0;
                    $info["data"] = $body["data"];
                }else{
                    $info["error_info"] = $body["data"];
                }
            }

        } else {
            $info["error_info"] = $result["error_info"];
        }
        return $info;
    }

    static function curl_url($url, $headers=[], $body=[])
    {
        $result = ["code" => 1, "body" => "", "error_info" => ""];
        try {
            $ch = curl_init();
            //设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            if (!empty($headers))
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_POST, 1);
            if (!empty($body)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            }
            //执行并获取HTML文档内容
            $output = curl_exec($ch);
//            print_r($output);
            if ($output === false) {
                $result["error_info"] = curl_errno($ch);
            }
            $result["code"] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $result["body"] = $output;
            //释放curl句柄
            curl_close($ch);
        } catch (\Exception $e) {
            $result["error_info"] = $e->getMessage();
        }
        return $result;
    }
    static function ToUrlParams($urlObj)
    {
        $buff = "";
        foreach ($urlObj as $k => $v)
        {
            if($k != "sign"){
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
}